package Model;

import util.Constants;
import util.Player;
import util.TicTacToeException;

class Board {
	
	
	public Player[][] theboard;
	
	
	public Board() {
		theboard = new Player[Constants.Board_Size][Constants.Board_Size];
		

	}
	
	
	public void makeMove(int x, int y, Player playerturn) throws TicTacToeException {
		if(x>=Constants.Board_Size) {
			throw new TicTacToeException("Aha Ahahahaha...Good one, but please try again!\nx or " + x + " index exceeds board size");
		}
		if(y>=Constants.Board_Size) {
			throw new TicTacToeException("Aha Ahahahaha...Noooo nooooo, you can't go there!\ny or " + y + " index exceeds board size");
		}
		if(theboard[x][y] != null) {
			throw new TicTacToeException("Aha Ahahahaha...No no no, not today!");
		}
		theboard[x][y] = playerturn;
		
		
		
	}
	
	
	public Player checkWin(Player o) {
		// side
		Player q;
		Player h = o;

		if((theboard[0][0] == theboard[0][1]) &&  (theboard[0][1] == theboard[0][2]) && (theboard[0][0]==(h))) {             
			 q = h;
		} else if((theboard[1][0] == theboard[1][1]) &&  (theboard[1][1] == theboard[1][2]) && (theboard[1][0]== h)) {
			q = h;
		} else if(theboard[2][0] == theboard[2][1] &&  theboard[2][1] == theboard[2][2] && theboard[2][0]==h) {
			q = h;
			
			// down
			
		} else if(theboard[0][0] == theboard[1][0] &&  theboard[1][0] == theboard[2][0] && theboard[0][0]== h) {             
			q = h;
		} else if(theboard[0][1] == theboard[1][1] &&  theboard[1][1] == theboard[2][1] && theboard[0][1]== h) {
			q = h;
		} else if(theboard[0][2] == theboard[1][2] &&  theboard[1][2] == theboard[2][2] && theboard[0][2]==h) {
			q = h;
		} else 
		
		//diagonal 
		if(theboard[0][0] == theboard[1][1] &&  theboard[1][1] == theboard[2][2] && theboard[0][0]== h) {             
			q = h;
		} else if(theboard[2][0] == theboard[1][1] &&  theboard[1][1] == theboard[0][2] && theboard[2][0]==h) {
			q = h;
		} else {
			q = null;
		}

		return q;
		}
	
	


	public void printBoard() {
		// TODO Auto-generated method stub
		for(int i=0;i<Constants.Board_Size;i++) {
			for(int j=0;j<Constants.Board_Size;j++) {
				if(theboard[i][j]!=null) {
				System.out.print(theboard[i][j].toString());
				}
				System.out.print("| ");
				
				
			}
			System.out.println();
		}
		
	}
	
	
	
}
